問題一、RegExp輸出的方式有哪些?
前一天有講到test()的輸出方式,今天我們就來看看其他的方式吧!
const regex1 = RegExp('foo*', 'g');
const str1 = 'table football, foosball';
let array1;
while ((array1 = regex1.exec(str1)) !== null) {
console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
// Expected output: "Found foo. Next starts at 9."
// Expected output: "Found foo. Next starts at 19."
}
*
符號為正規表達式,代表o字母可以出現零次獲多次。array1
,用於存儲每次匹配後的結果。[0]
} 代表 解析出來的匹配完整值。Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match